package org.linlinjava.litemall.wx.util;
/**
 快递物流查询接口
  商品购买地址：https://market.aliyun.com/products/56928004/cmapi022273.html
 String host = "http://kdwlcxf.market.alicloudapi.com"; //服务器
 String path = "/kdwlcx"; //接口地址
 */


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.linlinjava.litemall.db.domain.Fegine;
import org.linlinjava.litemall.db.domain.FegineList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FegineApi {
    //快递单号为空
    public static final String  ORDER_NULL="201";
    //快递公司为空
    public static final String  COMPANY_NULL="202";
    //快递公司不存在
    public static final String  COMPANY_NOT_EXIST="203";
    //快递公司识别失败
    public static final String  COMPANY_ERROR="204";
    //没有信息；单号错误 (正确单号应是扫描入库的正规单号)
    public static final String  ORDER_ERROR ="205";
    //该单号被限制，错误单号(连续错误查询，该单号会被屏蔽4小时);一个单号对应多个快递公司，请求须指定快递公司
    public static final String  ORDER_LIMIT ="207";
    //快递单号为空
    public static final String  ORDER_SUCESS="0";



    public static Fegine getFegine(String number,String type) {
        String host = "http://kdwlcxf.market.alicloudapi.com";
        String path = "/kdwlcx";
        String method = "GET";
        // !!! 替换这里填写你自己的AppCode 请在买家中心查看
        String appcode = "ba8d1ed41824413f89f4871859de62dc";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("no", number);
        querys.put("type", type);
        Fegine f = new Fegine();
        try {

            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            //System.out.println(response.toString()); //输出头部
            //System.out.println(EntityUtils.toString(response.getEntity())); //输出json
          /*  String i = "{\"status\":\"1\",\"msg\":\"ok\",\"result\":{\"number\":\"820026695306\",\"type\":\"sfexpress\",\n" +
                    "\"list\":[],\"deliverystatus\":\"3\",\"issign\":\"1\",\"expName\":\"顺丰快递\",\"expSite\":\"www.sf-express.com \",\"expPhone\":\"95338\"}}\n";
           */
            JSONObject o = JSONObject.fromObject(EntityUtils.toString(response.getEntity()));
            if(ORDER_SUCESS.equals(o.get("status"))){
                JSONObject result = (JSONObject)o.get("result");
                f.setStatus(Fegine.SUCCESS);
                f.setExpname(result.get("expName")==null?null:result.get("expName").toString());
                f.setNumber(result.get("number")==null?null:result.get("number").toString());
                f.setType(result.get("type")==null?null:result.get("type").toString());
                f.setList(result.get("list")==null?null:result.get("list").toString());
            }else{
                f.setStatus(Fegine.ERROR);
            }
           return f;
        } catch (Exception e) {
            e.printStackTrace();
            f.setStatus(Fegine.ERROR);
            return f;
        }
    }
}
